import { getENV } from "@/utils/env"

export default {
    /**
       * @description 解析地址栏参数
       * @param url
       * @return {Object}
       */
    parseUrlParams(url) {
        if (!url) {
            let hash = window.location.hash;
            let search = window.location.search;

            if (hash.indexOf('?') !== -1) {
                search = search.replace('?', '&');
            }
            url = hash + search;
        }
        let paramStr = url.split('?')[1];

        if (paramStr === undefined) {
            return {};
        }

        let paramArr = paramStr.split("&");
        let l = paramArr.length;
        let params = {};

        for (let i = 0; i < l; i++) {
            let tempArr = paramArr[i].split('=');

            params[tempArr[0]] = tempArr[1];
        }

        return params;
    },


    getLocalTime(tm, full) {
        var tm = tm - 0;
        var da = tm;
        da = new Date(da);
        var year = da.getFullYear();
        var month = da.getMonth() + 1;
        var date = da.getDate();
        var h = da.getHours();
        var m = da.getMinutes();
        var s = da.getSeconds();
        var datatime = year + '-' + month + '-' + date + ' ' + h + ':' + (m * 1 >= 10 ? m : "0" + m * 1) + ':' + s
        return this.formatTime(datatime, full)
    },

    formatTime(s, full) {
        var arr = s.split(" ");
        var date = arr[0].split("-");
        var time = arr[1].split(":");
        var y = date[0];
        var m = date[1];
        var d = date[2];
        var _string = new Date();
        var _y = _string.getFullYear();
        var _m = _string.getMonth() + 1;
        var _d = _string.getDate();
        if (y == _y && m == _m && d == _d) {
            var _hours = Number(time[0]);
            var _hours_str = (_hours < 12) ? (_hours < 6 ? "凌晨 " + _hours : "上午 " + _hours) : ((_hours > 12) ? (_hours >= 18 ? "晚上 " + (_hours - 12) : "下午 " + (_hours - 12)) : "中午 12");
            return _hours_str + ":" + time[1];
        } else {
            // 昨天的消息
            if (y == _y && m == _m && (_d - d) === 1) {
                return full ? "昨天" + " " + time[0] + ":" + time[1] : (y + '/' + m + '/' + d);
            }
            //console.log("不是同一天发的 直接显示 "+ y.toString().substring(2) +"-"+ m +"-"+ d);
            return full ? (y + "年" + m + "月" + d + "日" + " " + time[0] + ":" + time[1]) : (y + '/' + m + '/' + d);
        }
    },


    getCookie(name) {
        var cookie_start = document.cookie.indexOf(name + "="), cookie_end = document.cookie.indexOf(";", cookie_start);
        var cookieValue = cookie_start == -1 ? "" : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
        return encodeURIComponent(cookieValue);
    },

    throttle(action, delay = 600) {
        let statTime = 0;
        return function () {
            let currTime = +new Date();
            if (currTime - statTime > delay) {
                action.apply(this, arguments);
                statTime = currTime;
            }
        }
    },
    debounce(fn, delay = 600) {
        let timerId;
        return function (...args) {
            if (timerId) {
                clearTimeout(timerId);
            }
            timerId = setTimeout(() => {
                fn.apply(this, args);
            }, delay);
        }
    },
    isUndef(target) {
        return typeof target === "undefined"
    },
    /**
     * 是否是pdf链接
     * @param {*} url 
     * @returns 
     */
    isPdfLink(url) {
        return /\.pdf(\?.*)?$/i.test(url);
    },
    previewPDF(url) {
        if (this.isPdfLink(url)) {
            window.open(`https://assets.${getENV()}geilicdn.com/v-components/cdn/pdfjs-dist/1.9.426/web/viewer.html?file=${encodeURIComponent(url)}`)
        } else {
            window.open(url)
        }
    }
}
